package com.example.controller;

import com.example.common.Result;
import com.example.entity.market.Market;
import com.example.service.UserService;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/market")
public class MarketController {
    @Resource
    private UserService userService;

    //查询行情
    @GetMapping("/selectMarket")
    public Result selectMarket(Market market) {
        List<Market> list = userService.selectMarket(market);
        return Result.success(list);
    }

    //分页行情查询
    @GetMapping("/selectMarketPage")
    public Result selectMarketPage(Market market,
                                   @RequestParam(defaultValue = "1") Integer pageNum,
                                   @RequestParam(defaultValue = "10") Integer pageSize) {
        PageInfo<Market> page = userService.selectMarketPage(market, pageNum, pageSize);
        return Result.success(page);
    }

    //热门行情查询
    @GetMapping("/selectMarketHot")
    public Result selectMarketHot(Market market) {
        List<Market> list = userService.selectMarketHot(market);
        return Result.success(list);
    }

    @GetMapping("/getMarketsByProvince")
    public ResponseEntity<Map<String, Object>> getMarketsByProvince(@RequestParam String province) {
        List<Market> markets = userService.getMarketsByProvince(province);
        Map<String, Object> response = new HashMap<>();

        if (markets.isEmpty()) {
            response.put("code", "204");
            response.put("msg", "没有该省份的市场数据");
            response.put("data", new ArrayList<>());
        } else {
            response.put("code", "200");
            response.put("msg", "请求成功");
            response.put("data", markets);
        }

        return ResponseEntity.ok(response);
    }
}
